package com.kingdee.eas.custom.aoyuan.aytime.bizbill.app;

import org.apache.log4j.Logger;
import javax.ejb.*;
import java.rmi.RemoteException;
import com.kingdee.bos.*;
import com.kingdee.bos.util.BOSObjectType;
import com.kingdee.bos.metadata.IMetaDataPK;
import com.kingdee.bos.metadata.query.util.CompareType;
import com.kingdee.bos.metadata.rule.RuleExecutor;
import com.kingdee.bos.metadata.MetaDataPK;
//import com.kingdee.bos.metadata.entity.EntityViewInfo;
import com.kingdee.bos.framework.ejb.AbstractEntityControllerBean;
import com.kingdee.bos.framework.ejb.AbstractBizControllerBean;
//import com.kingdee.bos.dao.IObjectPK;
import com.kingdee.bos.dao.IObjectValue;
import com.kingdee.bos.dao.IObjectCollection;
import com.kingdee.bos.service.ServiceContext;
import com.kingdee.bos.service.IServiceContext;

import com.kingdee.eas.custom.aoyuan.aytime.bizbill.OTBillCollection;
import com.kingdee.eas.custom.aoyuan.aytime.bizbill.OTBillEntryCollection;
import com.kingdee.eas.custom.aoyuan.aytime.bizbill.OTBillEntryInfo;
import com.kingdee.eas.custom.aoyuan.aytime.bizbill.PersonOTBalanceCollection;
import com.kingdee.eas.custom.aoyuan.aytime.bizbill.PersonOTBalanceFactory;
import com.kingdee.eas.custom.aoyuan.aytime.bizbill.PersonOTBalanceInfo;

import java.lang.String;
import java.math.BigDecimal;

import com.kingdee.bos.metadata.entity.EntityViewInfo;
import com.kingdee.bos.metadata.entity.FilterInfo;
import com.kingdee.bos.metadata.entity.FilterItemInfo;
import com.kingdee.eas.common.EASBizException;
import com.kingdee.bos.dao.IObjectPK;
import com.kingdee.bos.metadata.entity.SelectorItemCollection;
import com.kingdee.eas.custom.aoyuan.aytime.AYAttenBillStatusEnum;
import com.kingdee.eas.custom.aoyuan.aytime.bizbill.OTBillInfo;

public class OTBillControllerBean extends AbstractOTBillControllerBean
{
    private static Logger logger =
        Logger.getLogger("com.kingdee.eas.custom.aoyuan.aytime.bizbill.app.OTBillControllerBean");
    
    /**
     * 提交加班单后，更新或新增加班调休情况表
     */
	protected IObjectPK _submit(Context ctx, IObjectValue objectValue)
			throws BOSException, EASBizException {	
		return super._submit(ctx, objectValue);
	}
	
	public void sumPersonOTBalance(Context ctx,OTBillInfo billInfo,PersonOTBalanceInfo oTBalanceInfo,String sumType) throws BOSException, EASBizException
	{
		OTBillEntryCollection OTEntryCollection =billInfo.getEntrys();
		for(int i=0;i<OTEntryCollection.size();i++)
		{
			OTBillEntryInfo entry=OTEntryCollection.get(i);
			if(entry.getOtType()==null)
			{
				return;
			}
			if(entry.getOtType().getValue()==1)
			{
				//工作日加班
				BigDecimal totalHour=entry.getTotalHour();
				if(totalHour==null)
				{
					totalHour=new BigDecimal("0");
				}
				BigDecimal oldNormalOT=oTBalanceInfo.getNormalOT();
				if(oldNormalOT==null)
				{
					oldNormalOT=new BigDecimal("0");
				}
				if(sumType.equals("1"))
				{
					//添加
					oTBalanceInfo.setNormalOT(oldNormalOT.add(totalHour));
				}else if(sumType.equals("2"))
				{
					//返回
					oTBalanceInfo.setNormalOT(oldNormalOT.subtract(totalHour));
				}
			}else if(entry.getOtType().getValue()==2)
			{
				//周末加班
				BigDecimal totalHour=entry.getTotalHour();
				if(totalHour==null)
				{
					totalHour=new BigDecimal("0");
				}
				BigDecimal oldWeekenOT=oTBalanceInfo.getWeekenOT();
				if(oldWeekenOT==null)
				{
					oldWeekenOT=new BigDecimal("0");
				}
				if(sumType.equals("1"))
				{
					//添加
					oTBalanceInfo.setWeekenOT(oldWeekenOT.add(totalHour));
				}else if(sumType.equals("2"))
				{
					//返回
					oTBalanceInfo.setWeekenOT(oldWeekenOT.subtract(totalHour));
				}					
			}else if(entry.getOtType().getValue()==3)
			{
				//节假日加班
				BigDecimal totalHour=entry.getTotalHour();
				if(totalHour==null)
				{
					totalHour=new BigDecimal("0");
				}
				BigDecimal oldHolidayOT=oTBalanceInfo.getHolidayOT();
				if(oldHolidayOT==null)
				{
					oldHolidayOT=new BigDecimal("0");
				}
				if(sumType.equals("1"))
				{
					//添加
					oTBalanceInfo.setHolidayOT(oldHolidayOT.add(totalHour));
				}else if(sumType.equals("2"))
				{
					//返回
					oTBalanceInfo.setHolidayOT(oldHolidayOT.subtract(totalHour));
				}							
			}	
		}
		PersonOTBalanceFactory.getLocalInstance(ctx).save(oTBalanceInfo);
	}
	
	public PersonOTBalanceCollection getPersonOTBalanceCollection(Context ctx,OTBillInfo billInfo) throws Exception{
		if(billInfo==null)
		{
			return null;
		}
		EntityViewInfo evi = new EntityViewInfo();
		FilterInfo filter = new FilterInfo();
		if (billInfo.getEmp() != null) {
			filter.getFilterItems()
					.add(
							new FilterItemInfo("person.id", billInfo.getEmp().getId().toString(),
									CompareType.EQUALS));
		}else
		{
			return null;			
		}
		evi.setFilter(filter);
		SelectorItemCollection sic = new SelectorItemCollection();
		sic.add("*");
		evi.setSelector(sic);
		return PersonOTBalanceFactory.getLocalInstance(ctx).getPersonOTBalanceCollection(evi);
	}
}